package com.oocl.consume.customer;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.oocl.consume.parsetoobject.ParseToObject;

public class CustomerPtpAsyn{
	public final static List<String> list = new ArrayList<String>();
	public static void customer()throws Exception {
		ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Destination queue=new ActiveMQQueue("wyatt");
		Connection con=factory.createConnection();
		con.start();
		
		final Session sen=con.createSession(true, Session.CLIENT_ACKNOWLEDGE);
		final MessageConsumer consumer=sen.createConsumer(queue);
		//block thread
		consumer.setMessageListener(new MessageListener() {

			public void onMessage(Message arg0) {
				
				try {
					synchronized (sen) {
						TextMessage msg=(TextMessage)arg0;
						String msg1 =msg.getText();
						sen.commit();
					
					System.out.println(msg1);
					try {
						ParseToObject.parseToBookList(msg1);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					}
					//list.add(msg1);
					System.out.println(list.size());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
	System.out.println("main end.....");
	}
	public static void main(String[] args) throws Exception {
		customer();
	}
}